
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aliyuncs.utils.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.CommentDao;
import com.sun.showcase.biz.mall.service.CommentService;
import com.sun.showcase.biz.mall.service.MemberService;
import com.sun.showcase.biz.mall.service.OrderGoodsService;
import com.sun.showcase.biz.mall.service.OrderService;
import com.sun.showcase.client.domain.mall.Comment;
import com.sun.showcase.client.domain.mall.Member;
import com.sun.showcase.client.query.mall.CommentQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
@Service("commentService")
@Transactional
public class CommentServiceImpl implements CommentService{
	@Autowired
	private CommentDao commentDao;
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderGoodsService orderGoodsService;
	@Autowired
	private MemberService memberService;
	
	public void setCommentDao(CommentDao dao) {
		this.commentDao = dao;
	}

	public DataGrid datagrid(CommentQuery commentQuery) {
		DataGrid j = new DataGrid();
		Pager<Comment> pager  = find(commentQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<CommentQuery> getQuerysFromEntitys(List<Comment> comments) {
		List<CommentQuery> commentQuerys = new ArrayList<CommentQuery>();
		if (comments != null && comments.size() > 0) {
			for (Comment tb : comments) {
				CommentQuery b = new CommentQuery();
				BeanUtils.copyProperties(tb, b);
				commentQuerys.add(b);
			}
		}
		return commentQuerys;
	}

	private Pager<Comment> find(CommentQuery commentQuery) {
		Page<Comment> page = PageHelper.startPage(commentQuery.getPage().intValue(), commentQuery.getRows().intValue());
		List<Comment> list = commentDao.findList(commentQuery);
		Pager<Comment> pager = new Pager<Comment>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Comment add(CommentQuery commentQuery) {
		Comment t = new Comment();
		BeanUtils.copyProperties(commentQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
	    t.setShowStatus(StringConstant.ONE);
		commentDao.save(t);
		commentQuery.setId(t.getId());
		return t;
		
	}

	public void update(CommentQuery commentQuery) {
		Comment t = commentDao.getById(commentQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(commentQuery, t);
	    	commentDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(CommentQuery commentQuery) {
	    commentDao.updatePart(commentQuery);
	}

	public void delete(java.lang.String[] ids) {
		commentDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		commentDao.deletePt(ids);
	}

	public Comment get(CommentQuery commentQuery) {
		return commentDao.getById(commentQuery.getId());
	}

	public Comment get(String id) {
		return commentDao.getById(id);
	}

	
	public List<CommentQuery> listAll(CommentQuery commentQuery) {
	    List<Comment> list = commentDao.findList(commentQuery);
		List<CommentQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public ExecuteResult<Comment> save(CommentQuery commentQuery) {
		ExecuteResult<Comment> exe = new ExecuteResult<Comment>();
		if(StringUtils.isEmpty(commentQuery.getUserId())
				||StringUtils.isEmpty(commentQuery.getShopId())
				||StringUtils.isEmpty(commentQuery.getGoodsId())
				||StringUtils.isEmpty(commentQuery.getSkuId())
				||StringUtils.isEmpty(commentQuery.getOrderId())
				) {
			exe.addErrorMessage("无效评价");
			return exe;
		}
		Member mem = memberService.get(commentQuery.getUserId());
		if(mem == null) {
			exe.addErrorMessage("无效评价");
			return exe;
		}
		CommentQuery q = new CommentQuery();
		q.setUserId(commentQuery.getUserId());
		q.setOrderId(commentQuery.getOrderId());
		q.setGoodsId(commentQuery.getGoodsId());
		List<CommentQuery> list = this.listAll(q);
		if(list.size()>0) {
			exe.addErrorMessage("您已评价过了！");
			return exe;
		}
		commentQuery.setHeadImage(mem.getHeadImage());
		Comment c = this.add(commentQuery);
		exe.setResult(c);
		orderGoodsService.comment(c.getOrderId(), c.getGoodsId());
		orderService.comment(c.getOrderId());
		return exe;
	}
	
	
}
